<template>
<div>
    <Input v-model="filter.name">
        <Button slot="append" icon="ios-search" @click="search"></Button>
    </Input>
    <Button type="primary" @click="addData" style="margin:5px;">添加评论</Button>
    <Button type="error"  @click="removeData" style="margin:5px;">删除选中</Button>    
    <Table :columns="columns1" :data="list" @on-selection-change="removeData"></Table>
    <Page :total="filter.total" show-elevator show-total size="small" show-sizer :page-size="1" @on-page-size-change="onPageSizeChange"  @on-change="changePage"></Page>    
    <Modal
        v-model="modal1"
        title="普通的Modal对话框标题"
        @on-ok="ok"
        @on-cancel="cancel">
                                                                    <!-- :rules="ruleValidate" -->
        <Form ref="formValidate" :model="formValidate"  :label-width="80">
            <FormItem label="评论人" prop="people">
                <Input v-model="formValidate.people" placeholder="请输入姓名"></Input>
            </FormItem>
            <FormItem label="评论内容" prop="content">
                <Input v-model="formValidate.content" placeholder="请输入评论"></Input>
            </FormItem>
             <FormItem label="评分" prop="value">
                <Rate v-model="formValidate.value"></Rate>
            </FormItem>
            <FormItem label="选择日期">
                <Row>
                    <Col span="11">
                        <FormItem prop="date">
                            <DatePicker type="date" placeholder="选择日期" v-model="formValidate.date"></DatePicker>
                        </FormItem>
                    </Col>
                    <Col span="2" style="text-align: center">-</Col>
                    <Col span="11">
                        <FormItem prop="time">
                            <TimePicker type="time" placeholder="选择时间" v-model="formValidate.time"></TimePicker>
                        </FormItem>
                    </Col>
                </Row>
            </FormItem>
           
            <FormItem>
                <Button type="primary" @click="handleSubmit('formValidate')">提交</Button>
                <Button type="ghost" @click="handleReset('formValidate')" style="margin-left: 8px">重置</Button>
            </FormItem>
        </Form>

    </Modal>

</div>
</template>
<script>
    export default {
        data () {
            return {
                modal1:false,//对话框
                list:[],
                filter:{
                    page:1,
                    rows:5,
                    name:'',
                    total:0
                },
                formValidate: {
                    people: '',
                    content: '',
                    date:'',
                    new_id:'59e820131e9c981ce41c7d9f',
                    value:null
                },
                columns1: [
                    {
                        type: 'selection',
                        width: 60,
                        align: 'center'
                    },
                    {
                        title: '评论人',
                        key: 'people',
                    },
                    {
                        title:'评论内容',
                        key:'content'
                    },
                    {
                        title:'评分',
                        key:'value',
                        render: (h, params) => {
                            return h('div', [
                                h('Rate', {
                                    props: {
                                        type: 'primary',
                                        size: 'small',
                                        value:params.row.value
                                    }
                                })
                            ]);
                        }
                    },
                    {
                        title:'评论日期',
                        key:'date'
                    },
                    {
                        title: '操作',
                        key: 'action',
                        width: 150,
                        align: 'center',
                        render: (h, params) => {
                            return h('div', [
                                h('Button', {
                                    props: {
                                        type: 'primary',
                                        size: 'small'
                                    },
                                    style: {
                                        marginRight: '5px'
                                    },
                                    on: {
                                        click: () => {
                                            // console.log(params)
                                            this.editData(params.row._id)
                                        }
                                    }
                                }, '编辑'),
                                h('Button', {
                                    props: {
                                        type: 'error',
                                        size: 'small'
                                    },
                                    on: {
                                        click: () => {
                                            this.removeData(params.row._id)
                                        }
                                    }
                                }, '删除')
                            ]);
                        }
                    }
                ],
                ruleValidate: {
                    name: [
                        { required: true, message: '姓名不能为空', trigger: 'blur' }
                    ],
                    email: [
                        { required: true, message: '邮箱不能为空', trigger: 'blur' },
                        { type: 'email', message: '邮箱格式不正确', trigger: 'blur' }
                    ],
                    date: [
                        { required: true, type: 'date', message: '请选择日期', trigger: 'change' }
                    ],
                    time: [
                        { required: true, type: 'date', message: '请选择时间', trigger: 'change' }
                    ]
                }
            }
        },
        methods:{
            handleSubmit (name) {
                this.$refs[name].validate((valid) => {
                    if (valid) {
                        console.log(this.formValidate)
                        //console.log(this.formValidate._id);
                        var id=this.formValidate._id;
                        //console.log(id);

                        if(id){
                            console.log('编辑模式')
                            this.$http({
                            method:'POST',
                            url:'http://localhost:3000/comment/data/'+id,
                            data:this.formValidate
                            }).then(res=>{
                                this.getData();
                            })
                        }

                        this.$http({
                            method:'POST',
                            url:'http://localhost:3000/comment/data',
                            data:this.formValidate
                        }).then(res=>{
                            this.getData();
                        })
                        this.$Message.success('提交成功!');
                    } else {
                        this.$Message.error('表单验证失败!');
                    }
                })
            },
            handleReset (name) {
                this.$refs[name].resetFields();
            },
            ok () {
                this.$Message.info('点击了确定');
            },
            cancel () {
                this.$Message.info('点击了取消');
            },

            onPageSizeChange(pageSize){
                this.filter.rows=pageSize;
                this.getData();
            },
            changePage(page){
                this.filter.page=page;
                this.getData();
            },
            //增加
            addData(){
                this.modal1=true;
                console.log(this.$route.path)
            },
            //删除一个或多个
            removeData(selection){
                console.log(selection)               
                if(Array.isArray(selection)){
                    var arr=[];
                    selection.forEach(function(obj) {
                        arr.push(obj._id)
                    }, this);
                    selection=arr
                }   
                this.$http({
                    method:'DELETE',
                    url:`http://localhost:3000/comment/data/`+selection
                }).then((response)=>{
                    this.getData();
                })
            },
            //修改数据
            editData(id){
                this.$http({
                    method:'GET',
                    url:`http://localhost:3000/comment/data/`+id,   //重新增加了 ：添加单个数据的node后台
                }).then((response)=>{                             //user.controller和users
                    console.log(response.data)
                    this.formValidate=response.data;
                })

                this.modal1=true;
            },
            //查找数据
            getData(){
                
               /*  this.$http({
                    method:'POST',
                    url:`http://localhost:3000/comment/list`,
                    data:this.filter
                })
                .then(res=>{
                    console.log(res.data);
                    this.list=res.data.rows;
                    this.filter.total=res.data.total;
                    //console.log(this.list)
                }) */
                //console.log(this.$route.query.routeParams)
                this.formValidate.new_id=this.$route.query.routeParams
                //alert(this.formValidate.new_id)
                 this.$http({
                    method:'POST',
                    url:'http://localhost:3000/comment/find',
                    data:{query:{new_id:this.formValidate.new_id}},
                }).then(res=>{
                    // console.log(res.data)
                    this.list = res.data;

                })
            },
            search(){
                this.getData();
            }
        },
        created(){
            this.getData();
        }
    }
</script>